/*
* Copyright (C) 2024 ~ 2025 Uniontech Technology Co., Ltd.
*
* Author:      zhangbingyi <zhangbingyi@uniontech.com>
*
* Maintainer:  zhangbingyi <zhangbingyi@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "resultdelegate.h"
#include "virusfiledetaildefinition.h"

#include <QPainter>
#include <QApplication>
#include <QMouseEvent>
#include <QToolTip>

//#include <DFontSizeManager>

ResultDelegate::ResultDelegate(QObject *parent)
    : VirusFileDetailDelegate(parent)
{
}

void ResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem itemOption(option);
    if (index.column() == COLUMN_ADDTIME) {
        // 绘制添加时间列
        painter->save();
        QString addTime = index.model()->data(index, ROLE_VIRUSFILE_ADDTIME).toString();
        int currentFontSize = qApp->font().pointSize();
        QFont font;
        font.setPixelSize(currentFontSize);
        QRect textRect = itemOption.rect;
        int heightSpacing = (textRect.height() - currentFontSize);
        QPoint textPoint(textRect.x() + 10, textRect.y() + heightSpacing);
        painter->setFont(font);
        QFontMetrics fontWidth(font);
        addTime = fontWidth.elidedText(addTime, Qt::ElideMiddle, itemOption.rect.width() - 10);
        painter->drawText(textPoint, addTime);
        painter->restore();
    }
    return VirusFileDetailDelegate::paint(painter, option, index);
}

bool ResultDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    QMouseEvent *mouse = static_cast<QMouseEvent *>(event);
    if (event->type() == QEvent::MouseButtonRelease && option.rect.contains(mouse->pos())) {
        bool checkState = model->data(index, ROLE_CHECKED_STATUS).toBool();
        model->setData(index, !checkState, ROLE_CHECKED_STATUS);
        Q_EMIT sendCheckClickSingal();
    }
    return true;
}

bool ResultDelegate::helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    if (event->type() == QEvent::ToolTip) {
        if (index.column() == COLUMN_ADDTIME) {
            QString tooltip;
            QStyleOptionViewItem itemOption(option);
            QRect rect = itemOption.rect;
            QRect addTimeRect(rect.x(), rect.y() + 20, itemOption.rect.width() - 10, option.fontMetrics.height());
            if (addTimeRect.contains(event->pos())) {
                tooltip = index.data(ROLE_VIRUSFILE_ADDTIME).toString();
            } else {
                QWidgetList qwl = QApplication::topLevelWidgets();
                for (QWidget *qw : qwl) {
                    if (QString("QTipLabel") == qw->metaObject()->className()) {
                        qw->close();
                    }
                }
            }
            QToolTip::showText(event->globalPos(), tooltip);
            return true;
        }
    }
    return VirusFileDetailDelegate::helpEvent(event, view, option, index);
}
